﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using ThapCam.Ulti;

/// <summary>
/// Summary description for ImageService
/// </summary>
[WebService(Namespace = "http://thapcam.vn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class ImageService : System.Web.Services.WebService
{

    public ImageService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }


    [WebMethod(Description = "Upload image file")]
    public string upLoadImageFile(byte[] bytes, string fileName)
    {
        try
        {
            MemoryStream stream = new MemoryStream(bytes);
            Image image = Image.FromStream(stream);

            int w = image.Width;
            int h = image.Height;
            if (w > 800)
            {
                float scale = (float)800 / w;
                w = 800;
                h = (int)Math.Ceiling(h * scale);
            }

            fileName = TCUtility.optimzeUrl(fileName).ToLower();
            fileName += "-" + TCUtility.randomNumberString(4) + ".jpg";
            string subfolder = String.Format("{0:MM-yyyy}", DateTime.Now);
            string folder = "~/" + ThapCam.System.WebConfig.PIC_FOLDER + subfolder;
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(Server.MapPath(folder));
            string imgPath = Server.MapPath(folder + "/" + fileName);

            Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics graphic = Graphics.FromImage((System.Drawing.Image)bmp);
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
            graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            graphic.Clear(Color.White);
            graphic.DrawImage(image, 0, 0, w, h);

            ImageCodecInfo jpegEncoderInfo = null;
            ImageCodecInfo[] encoderInfoArray = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo imageCodecInfo in encoderInfoArray)
            {
                if (0 == String.Compare("image/jpeg", imageCodecInfo.MimeType, true))
                {
                    jpegEncoderInfo = imageCodecInfo;
                    break;
                }
            }

            // If a JPEG encoder was found, use it to save a Bitmap to a JPEG file,
            // explicitly specifying a "quality" parameter of 100 (the maximum).
            if (null != jpegEncoderInfo)
            {
                EncoderParameters encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                bmp.Save(imgPath, jpegEncoderInfo, encoderParameters);
            }

            stream.Dispose();
            image.Dispose();
            graphic.Dispose();
            bmp.Dispose();
            return "200#" + ThapCam.System.WebConfig.PIC_FOLDER + subfolder + "/" + fileName;
        }
        catch (Exception ex) { return "500#" + ex.Message; }
    }


}
